import express from 'express'
export const router = express.Router()
import { createConnectionPool } from '../../mysql/index.js'
import * as PageUtil from '../../utils/pageutil.js'

const connection = await createConnectionPool()
function FormCheck(form) {
    const { pageSize, pageNum } = form
    if (!pageSize) {
        return {
            err: true,
            message: 'pageSize不能为空'
        }
    }
    if (!pageNum) {
        return {
            err: true,
            message: 'pageNum不能为空'
        }
    }
}
router.get("/channel/page", async (req, res) => {
    try {
        const form = req.query
        const verify = FormCheck(form)
        console.log(verify, 'verify');
        if (verify && verify.err) {
            res.status(500).json({ error: verify.message })
            return
        }
        const { id = null, pageSize = 10, pageNum = 1 } = req.query
        console.log(req.query, '222');
        const countSql = "select count(*) as total from channel where deleted = 0 and (? is null or id = ?)"
        const [countRows, countFields] = await connection.execute(countSql, [id, id])
        const total = countRows[0].total
        const page = PageUtil.toPage(total, pageSize, pageNum)
        let data = []
        if (page.hasVal) {
            const sql = "select * from channel where deleted = 0 and (? is null or id = ?) order by create_time desc limit ?,?"
            const [rows, fields] = await connection.query(sql, [id, id, page.limit, page.pageSize])
            data = rows
        }
        res.status(200).send({ code: 200, message: '获取数据成功', data, total })
    } catch (err) {
        console.error('Error fetching user:', err)
        res.status(500).json({ error: '获取数据失败' })
    }
})

function verifyForm(form) {
    const { name, code, icon, ck, channel_info_1, channel_info_2, channel_info_3, channel_info_4, pay_type, channel_type, account_duplication, restrict_time, order_time, nav_chart_num, query_func, ck_delay_func, code_func, save_time, pay_btn_num, single_fee_rate, } = form
    if (!name) {
        return {
            err: true,
            message: '通道名称不能为空'
        }
    }
    if (!code) {
        return {
            err: true,
            message: '通道编码不能为空'
        }
    }
    if (!icon) {
        return {
            err: true,
            message: '图标不能为空'
        }
    }
    if (!ck) {
        return {
            err: true,
            message: '取ck地址不能为空'
        }
    }
    if (!channel_info_1) {
        return {
            err: true,
            message: 'channel_info_1不能为空'
        }
    }
    if (!channel_info_2) {
        return {
            err: true,
            message: 'channel_info_2不能为空'
        }
    }


    if (!channel_info_3) {
        return {
            err: true,
            message: 'channel_info_3不能为空'
        }
    }
    if (!channel_info_4) {
        return {
            err: true,
            message: 'channel_info_4不能为空'
        }
    }

    if (!pay_type) {
        return {
            err: true,
            message: '支付方式不能为空'
        }
    }
    if (!channel_type) {
        return {
            err: true,
            message: '通道分类不能为空'
        }
    }
    if (!account_duplication) {
        return {
            err: true,
            message: '账号重复不能为空'
        }
    }
    if (!restrict_time) {
        return {
            err: true,
            message: '限制时间不能为空'
        }
    }
    if (!order_time) {
        return {
            err: true,
            message: '查单时间不能为空'
        }
    }
    if (!nav_chart_num) {
        return {
            err: true,
            message: '引导图数量不能为空'
        }
    }
    if (!query_func) {
        return {
            err: true,
            message: '查询方法不能为空'
        }
    }
    if (!ck_delay_func) {
        return {
            err: true,
            message: 'ck延长方法不能为空'
        }
    }
    if (!code_func) {
        return {
            err: true,
            message: '产码方法不能为空'
        }
    }
    if (!save_time) {
        return {
            err: true,
            message: '保存时间不能为空'
        }
    }
    if (!pay_btn_num) {
        return {
            err: true,
            message: '支付按钮数不能为空'
        }
    }
    if (!single_fee_rate) {
        return {
            err: true,
            message: '单独费率不能为空'
        }
    }

    return {
        err: false
    }
}

router.post("/channel/add", async (req, res) => {
    try {
        const form = req.body
        const verify = verifyForm(form)
        if (verify.err) {
            res.status(500).json({ error: verify.message })
            return
        }
        const sql = "insert into channel (name, code, icon, ck, channel_info_1, channel_info_2, channel_info_3, channel_info_4, pay_type, channel_type,account_duplication, restrict_time, order_time, nav_chart_num, query_func, ck_delay_func, code_func, save_time, pay_btn_num, single_fee_rate, create_time, deleted) values (?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        await connection.execute(sql, [form.name || null, form.code || null, form.icon || null, form.ck || null, form.channel_info_1 || null, form.channel_info_2 || null, form.channel_info_3 || null, form.channel_info_4 || null, form.pay_type || null, form.channel_type || null, form.account_duplication || null, form.restrict_time || null, form.order_time || null, form.nav_chart_num || null, form.query_func || null, form.ck_delay_func || null, form.code_func || null, form.save_time || null, form.pay_btn_num || null, form.single_fee_rate || null, new Date(), 0])
        res.status(200).send({ code: 200, message: '新增数据成功' })
    } catch (err) {
        console.error('Error adding user:', err)
        res.status(500).json({ error: '新增数据失败' })
    }
})

router.post("/channel/update", async (req, res) => {
    try {
        const form = req.body
        if (!form.id) {
            res.status(500).json({ error: 'id不能为空' })
            return
        }
        const verify = verifyForm(form)
        if (verify.err) {
            res.status(500).json({ error: verify.message })
            return
        }
        const sql = "update channel set name = ?, code = ?, icon = ?, ck = ?, channel_info_1 = ?, channel_info_2 = ?, channel_info_3 = ?, channel_info_4 = ?, pay_type = ?, channel_type = ?, account_duplication = ? , restrict_time = ?, order_time = ?, nav_chart_num = ?, query_func = ?, ck_delay_func = ?, code_func = ?, save_time = ?, pay_btn_num = ?, single_fee_rate = ? where id = ?"
        await connection.execute(sql, [form.name || null, form.code || null, form.icon || null, form.ck || null, form.channel_info_1 || null, form.channel_info_2 || null, form.channel_info_3 || null, form.channel_info_4 || null, form.pay_type || null, form.channel_type || null, form.account_duplication || null, form.restrict_time || null, form.order_time || null, form.nav_chart_num || null, form.query_func || null, form.ck_delay_func || null, form.code_func || null, form.save_time || null, form.pay_btn_num || null, form.single_fee_rate || null, form.id])
        res.status(200).send({ code: 200, message: '更新数据成功' })
    } catch (err) {
        console.error('Error updating user:', err)
        res.status(500).json({ error: '更新数据失败' })
    }
})

router.delete("/channel/delete", async (req, res) => {
    try {
        const { id } = req.query
        if (!id) {
            res.status(500).json({ error: 'id不能为空' })
            return
        }
        const sql = "update channel set deleted = 1 where id = ?"
        await connection.execute(sql, [id])
        res.status(200).send({ code: 200, message: '删除数据成功' })
    } catch (err) {
        console.error('Error deleting user:', err)
        res.status(500).json({ error: '删除数据失败' })
    }
})

export default router;